Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes a bug when >1 entities are added to and removed from 1 family in the same frame #59

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Eldalar
Copy link

@Eldalar Eldalar commented Jul 7, 2019

This should fix a small bug in the family code, namely one that occurs whenever the following is given:

  • A family contains 2 or more entities
  • 2 or more of those entities are deleted and added in the same frame

This bug then lead to only one of the entities to be properly updated.

The steps within the code, that lead to this problem are as following:

toRemove = {1, 2}
entities = {1(old), 2(old), 1(new), 2(new) }

During the first iteration it picks entities[0] {1(old}} and finds it in toRemove, deletes it from there and swaps it with entities[3]{ 2(new) }, leading to the following situation:

toRemove = {2}
entities = {2(new), 2(old), 1(new), 1(old) }

During the second iteration, it picks entities[0] {2(new)} and finds it in toRemove, deletes it from there and swaps it with entities[2] {1(new)}, leading to the final situation:

toRemove = {}
entities = {1(new), 2(old), 2(new), 1(old) }

Which then leads to 2(new) and 1(old) to be deleted

This change buffers the swaps in a vector, before executing them all at once, ensuring that the swaps already executed don't change the result of later forward scans through entities.

The bug would occur under the following circumstances:
* 2 Entities (1 and 2) belong to one family
* Both of those entities had an optional component added to them in the same frame
And it would lead to only one of the 2 entities to be updated

This lead to the following situation:
toRemove = { 1, 2 }
entities = { 1(old), 2(old), 1(new), 2(new) }
after one pass it would then lead to this situation:
toRemove = { 2 }
entities = { 2(new), 2(old), 1(new), 1(old) }
And on the second pass to:
toRemove = {}
entities = { 1(new), 2(old), 2(new), 1(old) }
Afterwards deleting the wrong entity with the ID 2.

The fix buffers the swaps in a vector and executes them all at once, preventing the changes from interfering with the forward scan.
Using reference instead of copy in iteration through moves
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant